/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package holidaysManager;

import app.Global;
import app.MyModel;
import entities.Holiday;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yoshi
 */
public class HolidayManagerModel implements MyModel {

    ArrayList<Holiday> holidays = new ArrayList<Holiday>();

    public ArrayList<Holiday> getHolidays() {
        return holidays;
    }

    public void initDefault() {
        clear();
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = connection.prepareStatement(Global.getHolidayQuery() + " WHERE date >= ? ORDER BY date");
            pst.setDate(1, new java.sql.Date(new java.util.Date().getTime()));
            rs = pst.executeQuery();
            while (rs.next()) {
                holidays.add(new Holiday(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(HolidayManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                pst.close();
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(HolidayManagerModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void initPrev(Holiday holiday) {
        clear();
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = connection.prepareStatement(Global.getHolidayQuery() + " WHERE date < ? ORDER BY date DESC TOP 20");
            pst.setDate(1, holiday.getDate());
            rs = pst.executeQuery();
            while (rs.next()) {
                holidays.add(new Holiday(rs));
            }

            if (holidays.isEmpty()) {
            } else {
            }
        } catch (SQLException ex) {
        } finally {
            try {
                rs.close();
                pst.close();
                connection.close();
            } catch (SQLException ex) {
            }
        }
    }

    public void initNext(Holiday holiday) {
        clear();
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = connection.prepareStatement(Global.getHolidayQuery() + " WHERE date > ? ORDER BY date DESC TOP 20");
            pst.setDate(1, holiday.getDate());
            rs = pst.executeQuery();
            while (rs.next()) {
                holidays.add(new Holiday(rs));
            }
        } catch (SQLException ex) {
        } finally {
            try {
                rs.close();
                pst.close();
                connection.close();
            } catch (SQLException ex) {
            }
        }
    }

    @Override
    public void clear() {
        this.holidays.clear();
    }
}
